﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using Autofac;
using Autofac.Integration.Mvc;
using System.Reflection;
using ImpServices;
using IServices;
using MVCCompany.Extensions;
namespace MVCCompany
{
    // 注意: 有关启用 IIS6 或 IIS7 经典模式的说明，
    // 请访问 http://go.microsoft.com/?LinkId=9394801

    public class MvcApplication : System.Web.HttpApplication
    {
        public static void RegisterGlobalFilters(GlobalFilterCollection filters)
        {
            filters.Add(new CustomExceptionAttribute(), 1);
            filters.Add(new HandleErrorAttribute(), 2);
        }

        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapRoute(
                "Default", // 路由名称
                "{controller}/{action}/{id}", // 带有参数的 URL
                new { controller = "Home", action = "Index", id = UrlParameter.Optional } // 参数默认值
            );

        }

        public void ReigsterOnApplicationStart()
        {

            var builder = new ContainerBuilder();//构造一个容器
            var iservices = Assembly.Load("IServices");
            //  builder.RegisterAssemblyTypes(iservices);

            var data = Assembly.Load("ImpServices");
            builder.RegisterAssemblyTypes(data).AsImplementedInterfaces().SingleInstance();
            builder.RegisterControllers(Assembly.GetExecutingAssembly());

            var container = builder.Build();
            DependencyResolver.SetResolver(new AutofacDependencyResolver(container));

        }

        protected void Application_Start()
        {

            AreaRegistration.RegisterAllAreas();
            ReigsterOnApplicationStart();
            RegisterGlobalFilters(GlobalFilters.Filters);
            RegisterRoutes(RouteTable.Routes);
        }
    }
}